<?php

/**
 * @author Murat Cem YALIN <muratcem@gmail.com>
 * @copyright 2014 muratcem.com - walkerPHP.com
 * @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
 * @version 1.0.1
 */

namespace core\Viewloader;

class view implements IView{
    private $viewName;
    private $withArray = array();

    function __construct($viewName){
        $viewName = str_replace('.', '/', $viewName);
        $this->viewName = 'pub/views/'.$viewName.'.html';
    }
    /**
     * function to set variables that are used in view files
     *
     * @param string|array $keyword
     * @param string $value
     * @return $this
     */
    public function with($keyword, $value = ''){
        if (is_array($keyword)){
            foreach($keyword as $key => $value){
                $this->withArray['[@'.$key.']'] = $value;
            }
        } else {
            $this->withArray['[@'.$keyword.']'] = $value;
        }
        return $this;
    }

    /**
     * function to change 2 dimension Array() like a Query Result with a list format
     *
     * @param string $keyword
     * @param array $array
     * @param string $rowFormatTemplate view name or row format string
     * @return $this
     */
    public function withList($keyword, Array $array, $rowFormatTemplate = ''){
        if (is_array($array) && count($array)){
            $rows = '';
            if ($rowFormatTemplate != ''){
                if (!strpos($rowFormatTemplate, '[@')){
                    $rowFormatTemplate = viewLoader::view($rowFormatTemplate)->getContent();
                }
                for ($i=0; $i<count($array); $i++){
                    $changeArray = array();
                    foreach($array[$i] as $key => $value){
                        $changeArray['[@'.$key.']'] = $value;
                    }
                    $rows .= strtr($rowFormatTemplate, $changeArray);
                }
            } else {
                //if there isn't any row format then just make it table row
                for ($i=0; $i<count($array); $i++){
                    $rows .= '<tr><td>'.implode('</td><td>', $array[$i]).'</td></tr>';
                }
            }
            $this->withArray['[@'.$keyword.']'] = $rows;
        }
        return $this;
    }

    /**
     * load view file contents
     *
     * @return string
     * @throws \Exception
     */
    private function loadFile(){
        if (file_exists($this->viewName)){
            return file_get_contents($this->viewName);
        } else {
            throw new \Exception('view file not found: '.$this->viewName);
        }
    }

    /**
     * render view and get result
     * @return string
     */
    public function getContent(){
        $content = '';
        if ($this->viewName != ''){
            $content = $this->loadFile();
            if (count($this->withArray)){
                $content = strtr($content, $this->withArray);
            }
        }
        return $content;
    }

    /**
     * render view and write result
     * @return void
     */
    public function writeContent(){
        echo $this->getContent();
    }




}